package mamikeji.liaowa.ui.activity.right.update;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MaxTextLength;

public class UpdateUserCodeActivity extends BaseActivity {
    public static final String TAG = "UpdateUserCodeActivity";
    private EditText editText_update_usercode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user_code);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.user_msg_update_code);
        setTitleRightBtn(R.string.user_msg_update_save, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(editText_update_usercode.getText().toString())) {
                    ForToast.showToast(R.string.user_msg_update_enmpy_code);
                    return;
                }
                if (editText_update_usercode.getText().toString().length() != 6) {
                    ForToast.showToast(R.string.user_msg_update_error_code);
                    return;
                }
                getUpdateCode(editText_update_usercode.getText().toString());
            }
        });

        editText_update_usercode = (EditText) findViewById(R.id.editText_update_usercode);

        InputFilter[] filters = new InputFilter[]{new MaxTextLength(6)};
        editText_update_usercode.setFilters(filters);
    }

    /**
     * 绑定邀请码
     */
    private void getUpdateCode(final String code) {
        showLoading();
        Map<String, String> formMap = new HashMap<>();
        formMap.put(HttpKey.activationcode, code);
        formMap.put(HttpKey.activationusercode, ForPreference.getString(ForContents.USER_CODE));
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.BingActivation, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        ForPreference.putString(ForContents.USER_activationcode,code);
                        finish();
                    }
                    ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    hideLoading();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
